package edu.gatech.geochat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
import edu.gatech.geochat.model.User;

public class TitleActivity extends BetterActivity {

	public static void launch(Activity source, User user) {
		if (user == null)
			return;
		GUISpace.put(TitleActivity.class, "user", user);
		Intent n = new Intent(source, TitleActivity.class);
		source.startActivity(n);
	}

	@Override
	public void onCreate(Bundle b) {
		super.onCreate(b);
		setContentView(R.layout.title);
		

        setupLocationStuff();
	}
	


	
	private void setupLocationStuff() {
		LocationManager locManager;
		Location currentLocation;
		locManager = (LocationManager) GeoChatActivity.getActiveContext().getSystemService(Context.LOCATION_SERVICE);
		//Looper.prepare();
		currentLocation = locManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		((User)GUISpace.get(TitleActivity.class, "user")).setLocation(currentLocation);
		Log.d("Initial Location", "Location set to: " + currentLocation);
		if (!locManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			Log.d("User location", "Provider does not work");
		}
		LocationListener listener = new LocationListener() {

			@Override
			public void onLocationChanged(Location location) {
				((User)GUISpace.get(TitleActivity.class, "user")).setLocation(location);
				((User)GUISpace.get(TitleActivity.class, "user")).updateCloseMessages();
			}

			@Override
			public void onProviderDisabled(String provider) {
				Toast.makeText(GeoChatActivity.getActiveContext(), "Provider has been disabled", Toast.LENGTH_SHORT);
			}

			@Override
			public void onProviderEnabled(String provider) {
				Toast.makeText(GeoChatActivity.getActiveContext(), "Provider has been enabled", Toast.LENGTH_SHORT);

			}

			@Override
			public void onStatusChanged(String provider, int status, Bundle extras) {

			}
		};

		SensorEventListener gyros = new SensorEventListener() {

			@Override
			public void onAccuracyChanged(Sensor sensor, int accuracy) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onSensorChanged(SensorEvent event) {

			}

		};

		// minDelay in milliseconds, minDistance in meters
		locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 100, 10, listener);
	}
	

	public void viewRadar(View view) {
		GeoChatActivity.launch(this, (User) GUISpace.get(getClass(), "user"));
	}

	public void placeMessage(View view) {
		AddNoteActivity.launch(this, (User) GUISpace.get(getClass(), "user"));
	}

	public void foundMessageHistory(View view) {
		FoundNotesActivity.launch(this, (User) GUISpace.get(getClass(), "user"));
	}

	public void placedMessageHistory(View view) {
		PlacedNotesActivity.launch(this, (User) GUISpace.get(getClass(), "user"));
	}

	public void settings(View view) {

	}
}
